OB.Emes_ActionButton = OB.Emes_ActionButton || {};

OB.Emes_ActionButton.pbomAudit = function(params, view) {
	var viewGrid = view.activeView.viewGrid;
	var record = viewGrid.getSelectedRecord();
	if (record) {
		isc.confirm("确定审核通过？", function(bool){
			if (bool) {
				var data = {
					recordId: record[OB.Constants.ID],
					type: "audit"
				};
				var callback = function(response, data, request){
					if(data.result){
						viewGrid.refreshGrid();
//						viewGrid.setEditValue(viewGrid.getRecordIndex(record), 'audit', true);
					}
					isc.showMessage(data.msg);
				};
				OB.RemoteCallManager.call("com.mes.handler.PbomAuditHandler", data, {}, callback);
			}
		});
	} else {
		isc.showMessage("请选择需审核记录！");
	}
};
OB.Emes_ActionButton.setPbomIsDefault = function(params, view) {
	var viewGrid = view.activeView.viewGrid;
	var record = viewGrid.getSelectedRecord();
	if (record) {
		isc.confirm("确定设置为默认？", function(bool){
			if (bool) {
				var data = {
					recordId: record[OB.Constants.ID],
					type: "default"
				};
				var callback = function(response, data, request){
					if(data.result){
						viewGrid.refreshGrid();
//						viewGrid.setEditValue(viewGrid.getRecordIndex(record), 'default', true);
					}
					isc.showMessage(data.msg);
				};
				OB.RemoteCallManager.call("com.mes.handler.PbomAuditHandler", data, {}, callback);
			}
		});
	} else {
		isc.showMessage("请选择记录！");
	}
};

isc.defineClass("EMES_ImportBomButton", "Button").addProperties({
	title : "导入BOM项",
	overflow : "visible",
	width : 100,
	layoutAlign : "center",
	showRollOver : false,
	showFocused : false,
	showDown : true,
	baseStyle: 'OBFormButton',
	titleStyle: 'OBFormButtonTitle',
	click : function() {
	}
});
isc.defineClass("EMES_DownBomButton", "Button").addProperties({
	title : "下载导入模板",
	overflow : "visible",
	width : 100,
	layoutAlign : "center",
	showRollOver : false,
	showFocused : false,
	showDown : true,
	click : function() {
	}
});
(function() {
	var importBomButton = {
		action : function() {
			var records = this.view.viewGrid.getSelectedRecords();
			var data = {};
			if (records.length > 0) {
				data = records[0];
				var recordId = data[OB.Constants.ID];
				OB.Layout.ClassicOBCompatibility.Popup.open('tree', 500, 400,
						OB.Application.contextUrl + 'web/com.mes/html/importBom.html?bomItemId=' + recordId, 
						'导入BOM项', window, true, true, true, null, false);
			} else {
				alert("请选择BOM项！");
			}
		},
		buttonType : 'import_bom',
		updateState : function() {
		}
	};
	var downBomTemplate = {
		action : function() {
			downFile(OB.Application.contextUrl + "file/downFile.html?getBasePath=true&" + 
						"filePath=web/com.mes/templates/bom_Item_Import_template.xlsx&fileName=BOM项导入模板.xlsx");
		},
		buttonType : 'down_bom_template',
		updateState : function() {
		}
	};
	//bom
	OB.ToolbarRegistry.registerButton(importBomButton.buttonType, isc.EMES_ImportBomButton, importBomButton, 1000, '15C97873F0494C33B1E3C8980F57B98B');
	OB.ToolbarRegistry.registerButton(downBomTemplate.buttonType, isc.EMES_DownBomButton, downBomTemplate, 999, '15C97873F0494C33B1E3C8980F57B98B');
}());

function downFile(url) {
	var frame = document.getElementById("Emes_Down_File_Frame");
	if (frame == null) {
		var div = document.createElement("div");
		document.body.appendChild(div);
		div.style.display = "none";
		div.innerHTML = ('<iframe style="display: none;" id="Emes_Down_File_Frame"></iframe>');
		frame = document.getElementById("Emes_Down_File_Frame");
	}
	frame.src = url;
}
